package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.enums.CommonStatusEnum;
import com.parking.mgr.useraccount.model.entity.BasicRoleUserEntity;
import com.parking.mgr.useraccount.model.mapper.BasicRoleUserMapper;
import com.parking.mgr.useraccount.service.BasicRoleUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BasicRoleUserServiceImpl
 * @Description TODO
 * @Auther: zhijianpu
 * @Date: 2023/6/6
 */
@Service
public class BasicRoleUserServiceImpl extends ServiceImpl<BasicRoleUserMapper, BasicRoleUserEntity> implements BasicRoleUserService {

    @Resource
    private BasicRoleUserMapper basicRoleUserMapper;

    @Override
    public List<String> getRoleUserList(String passportId) {
        QueryWrapper<BasicRoleUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_user_passport_id", passportId)
                .eq("f_enable_state", CommonStatusEnum.ENABLE.getCode());

        List<BasicRoleUserEntity> roleUserEntityList = basicRoleUserMapper.selectList(queryWrapper);
        ArrayList<String> roleIds = null;

        if (CollectionUtil.isNotEmpty(roleUserEntityList)) {
            roleIds = new ArrayList<>(roleUserEntityList.size());
            for (BasicRoleUserEntity roleUserEntity : roleUserEntityList) {
                roleIds.add(roleUserEntity.getFkBasicRoleId());
            }
        }

        return roleIds;

    }

    @Override
    public List<BasicRoleUserEntity> getRoleUserEntityList(String passportId) {
        QueryWrapper<BasicRoleUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_user_passport_id", passportId)
                .eq("f_enable_state", CommonStatusEnum.ENABLE.getCode());

        return basicRoleUserMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteBatchUserRolesById(List<String> ids) {
        basicRoleUserMapper.deleteBatchIds(ids);
    }

    @Override
    public String getOneByPassportId(String passportId) {

        LambdaQueryWrapper<BasicRoleUserEntity> queryWrapper = new LambdaQueryWrapper<BasicRoleUserEntity>()
                .eq(BasicRoleUserEntity::getFkUserPassportId, passportId);

        BasicRoleUserEntity basicRoleUserEntity = basicRoleUserMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNull(basicRoleUserEntity)) {
            return "";
        } else {
            return basicRoleUserEntity.getFkBasicRoleId();
        }

    }
}
